<template>
    <div class="teamplate-past">
        <div class="container-fluid">
            <div class="row h-query">
                <form class="form-inline">
                    <div class="biaoti">往日交易流水</div>
                    <div class="form-group">
                        <label class="">货位：</label>
                        <select class="form-control" id="j_huowei">
                            <option v-for="(item,index) in huowei" :value="item.hw">{{item.huowname}}</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="">开始日期：</label>
                        <input type="text" class="form-control start-date" id="j_startDate">
                    </div>
                    <div class="form-group">
                        <label class="">结束日期：</label>
                        <input type="text" class="form-control end-date" id="j_endDate">
                    </div>
                    <div class="form-group">
                        <label class="">综合搜索</label>
                        <input type="text" class="form-control" placeholder="请输入商品名称、流水号、商品编号" v-model="searchMsg">
                    </div>
                    <div class="form-group">
                        <button type="botton" class="btn btn-default" @click="oldWater">查询</button>
                    </div>
                    <div class="btn-group pull-right">
                        <button  type="botton"  class="btn btn-default"  @click="onExport"> 导出表格</button>
                    </div>
                </form>
            </div>
            <div class="row">
                <div class="list-table">
                    <div class="list-h item-hd">
                        <div class="btn-group">
                            <button class="btn" @click="onTab(0)">往日交易明细流水</button>
                            <button class="btn" @click="onTab(1)">处方登记核对</button>
                            <button class="btn" @click="onTab(2)">含麻登记核对</button>
                        </div>
                    </div>
                    <div class="item-bd">
                        <div class="table-wrap item">
                            <div class="table-box">
                                <table class="table table-striped tc">
                                    <tbody>
                                        <tr>
                                            <th style="text-align:left">流水号</th>
                                            <th style="text-align:left">日期</th>
                                            <th style="text-align:left">时间</th>
                                            <th style="text-align:right">比数</th>
                                            <th style="text-align:left">商品编号</th>
                                            <th style="text-align:left">商品名称</th>
                                            <th style="text-align:left">商品规格</th>
                                            <th style="text-align:left">包装单位</th>
                                            <th style="text-align:left">生产厂家</th>
                                            <th style="text-align:left">批号</th>
                                            <th style="text-align:left">有效期至</th>
                                            <th style="text-align:right">数量</th>
                                            <th style="text-align:right">零售价(元)</th>
                                            <th style="text-align:right">单价</th>
                                            <th style="text-align:right">扣率</th>
                                            <th style="text-align:right">应收金额</th>
                                            <th style="text-align:right">实收金额</th>
                                            <th style="text-align:right">成本单价</th>
                                            <th style="text-align:right">成本金额</th>
                                            <th style="text-align:right">毛利</th>
                                            <th style="text-align:right">毛利率</th>
                                            <th style="text-align:left">收款员</th>
                                            <th style="text-align:left">架位号</th>
                                        </tr>
                                        <tr v-for="(item,index) in list">
                                            <td style="text-align:left">{{item.lshh}}</td>
                                            <td style="text-align:left">{{item.rq}}</td>
                                            <td style="text-align:left">{{item.ontime}}</td>
                                            <td style="text-align:right">{{item.bishu}}</td>
                                            <td style="text-align:left">{{item.spbh}}</td>
                                            <td style="text-align:left">{{item.spmch}}</td>
                                            <td style="text-align:left">{{item.shpgg}}</td>
                                            <td style="text-align:left">{{item.dw}}</td>
                                            <td style="text-align:left">{{item.shengccj}}</td>
                                            <td style="text-align:left">{{item.pihao}}</td>
                                            <td style="text-align:left">{{item.sxrq}}</td>
                                            <td style="text-align:right">{{item.shl | formatPrice}}</td>
                                            <td style="text-align:right">{{item.lshj | formatPrice}}</td>
                                            <td style="text-align:right">{{item.dj | formatPrice}}</td>
                                            <td style="text-align:right">{{item.koul | formatPrice}}</td>
                                            <td  style="text-align:right">{{item.ysje | formatPrice}}</td>
                                            <td style="text-align:right">{{item.sshje | formatPrice}}</td>
                                            <td style="text-align:right">{{item.chbdj | formatPrice}}</td>
                                            <td style="text-align:right">{{item.chbje | formatPrice}}</td>
                                            <td style="text-align:right">{{item.ml | formatPrice}}</td>
                                            <td style="text-align:right">{{item.mll | formatPrice}}</td>
                                            <td style="text-align:left">{{item.shky}}</td>
                                            <td style="text-align:left">{{item.jwh}}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div class="b-balance clearfix has-border">
                                <div class="pull-left">应收金额
                                    <span class="blue">{{summary.ysjehj}}</span>
                                </div>
                                <div class="pull-left">实收金额
                                    <span class="pink">{{summary.sshjehj}}</span>
                                </div>
                                <div class="pull-left">成本合计 {{summary.chbjehj}}</div>
                                <div class="pull-left">抹零合计 {{summary.mlhj}}</div>
                            </div>
                            <div class="pager-box clearfix">
                                <div class="pull-left">共计
                                    <span class="blue">{{count}}</span>条信息 
                                    </div>
                                <div class="pull-right">
                                    <div id="pagenav8" class="tcdPageCode"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
                huowei: [],
                item: 0,
                list: [],
                summary: {},
                count: '',
                title: '往日交易流水明细',
                searchMsg: '',
                startDate: '',
                stopDate: '',
                hw: '',
                fdbs: '',
                urls: '',
            }
        },
        watch: {

        },
        methods: {
            onTab(num) { //表格切换
                let me = this;
                me.item = num;
                me.onTableList();
            },
            oldWater(e) { //查询
                let me = this
                me.initial();
            },
            check1(e) {
                let me = this;
                var a = /^(\d{4})-(\d{2})-(\d{2})$/
                if (!a.test($('.stopDate').val())) {
                    layer.msg("日期格式不正确!");
                    return false
                }
            },
            check2(e) {
                let me = this;
                var a = /^(\d{4})-(\d{2})-(\d{2})$/
                if (!a.test($('.startDate').val())) {
                    layer.msg("日期格式不正确!");
                    return false
                }
            },
            onExport(e) {
                let me = this;
                var downloadToken = +new Date();
                var _self = this;
                var url = me.urls + '?rq1=' + $('#j_endDate').val() + '&rq2=' + $('#j_startDate').val() + '&file=' + me.title + '&title=' + me.title + '&spmch=' + me.searchMsg + '&fdbs=' + me.fdbs + '&hw=' + $('#j_huowei option:selected').val() || '';

                var obj = {
                    time: downloadToken,
                    url: url,
                    _self: _self,
                }
                exportExcelForm(obj);
            },
            initial(curr) {
                let me = this;
                me.onTableList();
            },
            onTableList(e) { //查询所有接口
                let me = this;
                let curr;
                let parms = {
                    page: curr || 1,
                    rows: me.row,
                    spmch: me.searchMsg,
                    hw: $('#j_huowei option:selected').val() || '',
                    rq1: $('#j_endDate').val(),
                    rq2: $('#j_startDate').val(),
                    fdbs: me.fdbs
                }
                let paths = '';
                if (me.item == 0) {
                    paths = me.cfg.api + '/WRLSController/selectWRLS';
                    me.title = '往日交易流水明细';
                    me.urls = me.cfg.api + '/WRLSController/selectWRLSDY';
                }
                if (me.item == 1) {
                    paths = me.cfg.api + '/WRLSController/selectWRCFLS';
                    me.title = '处方登记核对';
                    me.urls = me.cfg.api + '/WRLSController/selectWRCFLSDY';
                }
                if (me.item == 2) {
                    paths = me.cfg.api + '/WRLSController/selectWRHMLLS';
                    me.title = '含麻登记核对';
                    me.urls = me.cfg.api + '/WRLSController/selectWRHMLLSDY';
                }

                $.ajax({
                    type: 'GET',
                    url: paths,
                    data: parms
                }).done(data => {
                    if (data.code == 200) {
                        me.list = data.data.list;
                        me.count = data.data.count;
                        if (data.data.list.length > 0) {
                            me.summary = data.data.sum;
                        } else {
                            for (let i in me.summary) {
                                me.$set(me.summary, i, 0);
                            }
                        }

                        laypage({
                            cont: 'pagenav9', //容器。值支持id名、原生dom对象，jquery对象,
                            pages: data.data.pagCount, //总页数
                            first: '第一页',
                            last: '最后一页',
                            curr: curr || 1, //当前页
                            jump: function(obj, first) { //触发分页后的回调
                                if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                                    me.initial(obj.curr);
                                }
                            }
                        });
                    }
                })
            }
        },
        mounted() {
            let me = this;
            me.fdbs = localStorage.getItem("fdbs");
            let user = JSON.parse(sessionStorage.getItem("user"));
            if (!user) {
                router.push({
                    path: "/login"
                })
                return;
            }
            let res = {
                pageName: 'MXLS_SHOW',
                sktid: localStorage.getItem('sktId')
            }

            let winHeight = $(window).outerHeight();
            if (winHeight <= 768) {
                me.row = 14
            } else if (winHeight <= 800) {
                me.row = 16
            } else if (winHeight <= 863) {
                me.row = 20
            } else {
                me.row = 22
            }
            $(".start-date,.end-date").datetimepicker({
                language: "zh-CN",
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0,
                format: "yyyy-mm-dd"
            });
            var time = new Date();
            let fullYear = time.getFullYear();
            let getMonth = time.getMonth() + 1;
            let getDate = time.getDate();
            if (getDate.toString().length == 2) {
                me.getDate = getDate;
            } else {
                me.getDate = '0' + getDate
            }

            // let time = new Date();
            let startTimes = time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + '01'; //默认开始日期
            let endTimes = time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + me.util.string.numFix(time.getDate()); //默认结束日期
            $('#j_startDate').val(startTimes);
            $('#j_endDate').val(endTimes);

            me.service.indexLsNew(me, res, data => {
                me.huowei = data.data.hwList;
                setTimeout(() => {
                    me.onTableList();
                }, 0)

            })

            $(document).keydown(e => {
                let key = e.keyCode;
                if (key == 13) {
                    let me = this
                    me.onTableList();
                }
            })
        }
    }
</script>